#ifndef BULLETDEBUGDRAWER_H
#define BULLETDEBUGDRAWER_H

// Qt
#include <QVector>
#include <QtGui/qvector3d.h>

// Bullet
#include "LinearMath/btIDebugDraw.h"
#include "LinearMath/btQuickprof.h"

// Bullet forward declarations
class btVector3;


/* Helper class; draws the world as seen by Bullet.
 * This is very handy to see if Bullet's world matches yours.
 * How to use this class :
 * Declare an instance of the class :
 * BulletDebugDrawer mydebugdrawer;
 * dynamicsWorld->setDebugDrawer(&mydebugdrawer);
 * Each frame, call it :
 * mydebugdrawer.SetMatrices(ViewMatrix, ProjectionMatrix);
 * dynamicsWorld->debugDrawWorld();
 *
 */
class BulletDebugDrawer : public btIDebugDraw
{
public:
   /*   void SetMatrices(glm::mat4 pViewMatrix, glm::mat4 pProjectionMatrix){
              glUseProgram(0); // Use Fixed-function pipeline (no shaders)
              glMatrixMode(GL_MODELVIEW);
              glLoadMatrixf(&pViewMatrix[0][0]);
              glMatrixMode(GL_PROJECTION);
              glLoadMatrixf(&pProjectionMatrix[0][0]);
      }*/

    virtual void drawLine(const btVector3& from,const btVector3& to,const btVector3& color);

    // These 3 have to be here to prevent abstract class instantiation error
    virtual void drawContactPoint(const btVector3& PointOnB,
    		const btVector3& normalOnB,btScalar distance,int lifeTime,const btVector3& color) {}

    virtual void reportErrorWarning(const char* warningString){}

    virtual void draw3dText(const btVector3& location,const char* textString) {}

    virtual void setDebugMode(int p)
    {
        mDebugMode = p;
    }

    int getDebugMode(void) const
    {
    	return mDebugMode;
    }

    QVector<QVector3D>& getVertices()
    {
    	return mLineVertices;
    }

    QVector<QVector3D>& getColors()
	{
    	return mLineColors;
	}

    QVector<QVector3D>& getTriangleVertices()
	{
		return mTriangleVertices;
	}

	QVector<QVector3D>& getTriangleColors()
	{
		return mTriangleColors;
	}

    void clearVertices()
    {
		mLineVertices.clear();
		mLineColors.clear();

		mTriangleVertices.clear();
		mTriangleColors.clear();
	}

    /*
     * Called while drawing sphere collision object - rasterize this !
     */
  /* virtual void drawSphere(btScalar radius, const btTransform& transform, const btVector3& color);
*/
	/*
	 * Called while drawing cube shape
	 * Does transform multiplication on cpu so not efficient. Getting opengl matrix and doing transform
	 * mul on gpu is better supposedly, though computing the OpenGL matrix may take up cpu anyway ?
	 * Look at GL_ShapeDrawer to see how to use the OpenGL matrix as the model matrix.
	 */
	virtual void drawBox(const btVector3& bbMin,
			             const btVector3& bbMax,
			             const btTransform& trans,
			             const btVector3& color);

private:
      int mDebugMode;
      QVector<QVector3D> mLineVertices;
      QVector<QVector3D> mLineColors;

      // Triangle vertices are stored separately
      QVector<QVector3D> mTriangleVertices;
      // 1 for each triangle added and in sequence(count = no. of vertices/3)
      QVector<QVector3D> mTriangleColors;

};

#endif // BULLETDEBUGDRAWER_H
